package org.apache.derby.iapi.services.monitor;

import org.apache.derby.iapi.security.SecurityUtil;
import org.apache.derby.shared.common.stream.HeaderPrintWriter;

/**
 * @author ex_chenzy59
 * @since 2025/3/6
 *
 */
public class Monitor {
    public static final Object syncMe = new Object();
    private static ModuleFactory monitor;
    private static boolean active;
    public static ModuleFactory getMonitorLite() {
        // Verify that we have permission to execute this method.
        SecurityUtil.checkDerbyInternalsPrivilege();

        synchronized (syncMe) {
            if (active && monitor != null)
                return monitor;
        }

        // initialize a monitor just to get system properties
        // with the right secuirty checks and the correct sematics
        // for lookup of derby.system.home.
        // This instance will be discarded once it is used.				;

        return new org.apache.derby.impl.services.monitor.FileMonitor();
    }
    public static void logMessage(String messageText) {
        getStream().println(messageText);
    }
    public static HeaderPrintWriter getStream() {
        return monitor.getSystemStreams().stream();
    }
    public static void logTextMessage(String messageID, Object... args) {
        getStream().println(MessageService.getTextMessage(messageID, args));
    }
    public static ModuleFactory getMonitor() {
        // Verify that we have permission to execute this method.
        SecurityUtil.checkDerbyInternalsPrivilege();

        return monitor;
    }
}
